package org.eclipse.wb.swt;

import org.eclipse.rap.rwt.RWT;
import org.eclipse.rap.rwt.client.service.JavaScriptExecutor;

public class BrowserUtil {

    /**
     * Instantiates a new BrowserHelper.
     */
    private BrowserUtil() { }

    /**
     * Gets the url from the current browser window.
     *
     * @return the current browser url
     */
    public static String getCurrentBrowserURL() {
        StringBuilder url = new StringBuilder();

        url.append("http://");
        url.append(RWT.getRequest().getLocalAddr());
        url.append(":");
        url.append(RWT.getRequest().getLocalPort());
        url.append(RWT.getRequest().getContextPath());
        url.append(RWT.getRequest().getServletPath());

        String encodedURL = RWT.getResponse().encodeURL(url.toString());
        return encodedURL;
    }

    /**
     * Opens the url in the current browser window.
     *
     * @param url the url
     */
    public static void openURL(String url) {
    	JavaScriptExecutor jsExecutor = RWT.getClient().getService(JavaScriptExecutor.class);
    	jsExecutor.execute("parent.window.location.href=\"" + url + "\";");
   }
}
